package org.dydl.controller.system;

import java.util.List;

import org.dydl.common.dto.ResponseMessage;
import org.dydl.model.system.Area;
import org.dydl.model.system.AreaList;
import org.dydl.model.system.AreaProductList;
import org.dydl.model.system.BusinessList;
import org.dydl.model.system.Product;
import org.dydl.service.system.AreaProductListService;
import org.dydl.service.system.IAreaService;
import org.dydl.service.system.IBusinessListService;
import org.dydl.service.system.IGoodsService;
import org.dydl.service.system.OrderDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 订单接口
 * 
 * @author 张华城
 *
 */
@RestController
@RequestMapping("/api/businessList")
public class BusinessListController {

	@Autowired
	private IBusinessListService businessListService;
	@Autowired
	private IAreaService areaService;
	@Autowired
	private AreaProductListService aplService;
	@Autowired
	private OrderDetailService orderDetailService;
	@Autowired
	private IGoodsService goodService;

	// private static final Logger LOGGER =
	// Logger.getLogger(UploadController.class);
	/**
	 * 列表
	 * 
	 * @param page
	 * @param rows
	 * @param orderNo
	 * @return
	 */
	@RequestMapping("/list")
	public ResponseMessage list(int page, int rows, String name) {

		ResponseMessage res = businessListService.list(page, rows, name);

		return res;
	}

	@RequestMapping("/save")
	public ResponseMessage save(@RequestBody BusinessList bl) {

		ResponseMessage res = businessListService.save(bl);

		return res;

	}

	/**
	 * 订单详情列表
	 * 
	 * @param page
	 * @param rows
	 * @param orderNo
	 * @return
	 */
	@RequestMapping("/detailList")
	public List<AreaList> detail(
			@RequestParam(value = "businessId", required = false) String businessId) {

		List<AreaList> list = areaService.list(businessId);

		return list;
	}

	@RequestMapping("/getProductList")
	public List<AreaProductList> getProductList(
			@RequestParam(value = "areaId", required = false) String areaId,
			@RequestParam(value = "businessId", required = false) String businessId) {

		if (areaId == null || areaId.equals("")) {
			return null; 			
		}else if (businessId == null || businessId.equals("")){
			return null;
		}

		Integer id = Integer.valueOf(areaId);

		List<AreaProductList> list = aplService.list(id,businessId);

		return list;
	}

	/**
	 * 自动匹配商品
	 */
	@RequestMapping("/get")
	public List<AreaProductList> get(
			@RequestParam(value = "name", required = false) String name) {
		List<AreaProductList> res = null;
		if (!name.isEmpty()) {
			res = aplService.get(name);
		}
		return res;
	}

	/**
	 * 自动匹配商品
	 */
	@RequestMapping("/getProduct")
	public List<Product> getArea(
			@RequestParam(value = "name", required = false) String name) {
		List<Product> res = null;
		if (!name.isEmpty()) {
			res = goodService.getGoodsByName(name);
		}
		return res;
	}

	@RequestMapping("/getArea")
	public List<Area> getProduct(
			@RequestParam(value = "name", required = false) String name) {
		List<Area> res = null;
		if (!name.isEmpty()) {
			res = areaService.getArea(name);
		}
		return res;
	}

	@RequestMapping("/saveAreaProduct")
	public ResponseMessage bothSave(@RequestBody List<AreaProductList> arr) {

		ResponseMessage res = aplService.saveAreaProduct(arr);

		return res;

	}

	@RequestMapping("/saveAreaList")
	public ResponseMessage saveAreaList(@RequestBody AreaList areaList) {

		ResponseMessage res = areaService.save(areaList);

		return res;

	}

	// /**
	// * 新增/修改
	// * @param orders
	// * @return
	// */
	// @RequestMapping("/bothSave")
	// public ResponseMessage save(@RequestBody OrdersVO ordersVO){
	//
	// return orderService.bothSave(ordersVO);
	// }

	// /**
	// * 删除
	// * @param ids
	// * @return
	// */
	// @RequestMapping("/delete")
	// public boolean delete(int[] ids){
	// if (ids.length != 0) {
	// for (int i = 0; i < ids.length; i++) {
	// orderService.delete(ids[i]);
	// }
	// return true;
	// } else {
	// return false;
	// }
	//
	// }

	/**
	 * 根据订单号查询订单
	 * 
	 * @param orderNo
	 * @return
	 */
	@RequestMapping("/findBybusinessIds")
	public ResponseMessage findBybusinessIds(
			@RequestParam(value = "businessId", required = false) String businessId) {

		return businessListService.findBybusinessIds(businessId);
	}

	@RequestMapping("findAllName")
	public List<Area> findAllName() {
		return businessListService.findAllName();
	}

	/**
	 * 订单详情列表
	 * 
	 * @param orderNo
	 * @return
	 */
	@RequestMapping("/getList")
	public ResponseMessage getList(
			@RequestParam(value = "businessId", required = false) String businessId) {
		ResponseMessage res = new ResponseMessage();
		try {
			List<AreaList> list = areaService.list(businessId);
			res.setSuccess(true);
			res.setData(list);
		} catch (Exception e) {
			res.setSuccess(false);
		}
		return res;
	}

	@RequestMapping("/deleteDetail")
	public ResponseMessage deleteDetail(@RequestBody String orderNo) {
		return orderDetailService.delete(orderNo);
	}

	/**
	 * 生成流水号
	 * 
	 * @return
	 */
	@RequestMapping("/serialNo")
	public String orderNo() {

		String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

		char random = chars.charAt((int) (Math.random() * 26));

		String serialNo = random + "" + System.currentTimeMillis();

		return serialNo;
	}

}
